package timing.ukulele.flow.controller;

import timing.ukulele.flow.service.FlowUserInfoService;
import timing.ukulele.flow.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import timing.ukulele.flow.vo.*;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @ClassName FlowUserInfoController
 * @Description TODO
 * @Author 30621
 * @Date 2021/2/23 15:28
 * @Version 1.0
 **/
@RestController
@RequestMapping("/userInfo")
@Api(tags = "流程获取地区、用户信息等接口")
public class FlowUserInfoController {

    @Autowired
    private FlowUserInfoService flowUserInfoService;

    @GetMapping("/getAreaTree")
    @ApiOperation("获取地区列表")
    public AreaVO getAreaList(@ApiParam("父节点code") @RequestParam(value = "parentcode", required = false) String parentcode) {
        return flowUserInfoService.getAreaTree(parentcode);
    }

    @GetMapping("/getDepTree")
    @ApiOperation("获取部门列表")
    public DeptmentVO getDepTree(@ApiParam("父节点code") @RequestParam(value = "parentcode", required = false) String parentcode) {
        return flowUserInfoService.getDepTree(parentcode);
    }

    @GetMapping("/getRoleList")
    @ApiOperation("获取角色列表")
    public List<RoleVO> getRoleList(@ApiParam("角色名称") @RequestParam(value = "name", required = false) String name) {
        return flowUserInfoService.getRoleList(name);
    }

    @GetMapping("/getFlowRuleList")
    @ApiOperation("获取流程规则")
    public List<RuleVO> getFlowRuleList() {
        return flowUserInfoService.getFlowRuleList();
    }

    @GetMapping("/getUserList")
    @ApiOperation("获取用户列表")
    public List<UserVO> getUserList(@ApiParam("防止参数不传") @RequestParam(value = "userpara", required = false) String userpara,
                                    @ApiParam("企业id") @RequestParam(value = "uuid", required = false) String uuid,
                                    @ApiParam("部门编码") @RequestParam(value = "depcode", required = false) String depcode,
                                    @ApiParam("区域编码") @RequestParam(value = "areacode", required = false) String areacode,
                                    @ApiParam("角色名称（逗号隔开）") @RequestParam(value = "roles", required = false) String roles,
                                    @ApiParam("角色是否是与的关系（1:是，0：否）") @RequestParam(value = "isand", required = false) String isand,
                                    @ApiParam("用户类型（1:政府,2:企业）") @RequestParam(value = "usertype", required = false) String usertype,
                                    @ApiParam("流程启动用户loginame") @RequestParam(value = "startuser", required = false) String startuser,
                                    @ApiParam("流程当前用户loginame") @RequestParam(value = "flowuser", required = false) String flowuser,
                                    @ApiParam("规则") @RequestParam(value = "rulecode", required = false) String rulecode) {
        return flowUserInfoService.getUserList(userpara, uuid, depcode, areacode, roles, isand, usertype, startuser, flowuser, rulecode);
    }


    @GetMapping("/getAreaTree/test")
    @ApiOperation("获取地区列表(测试)")
    public AreaVO getAreaListTest(@ApiParam("父节点code") @RequestParam(value = "parentcode", required = false) String parentcode) {
        return flowUserInfoService.getAreaTree(parentcode);
    }
   /* public AreaVO getAreaListTest() {
        String areaStr = getMockData("area.txt");
        if (StringUtils.isEmpty(areaStr))
            return null;
        ObjectMapper om = new ObjectMapper();
        try {
            AreaVO area = om.readValue(areaStr, AreaVO.class);
            return area;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }*/

    @GetMapping("/getDepTree/test")
    @ApiOperation("获取部门列表(测试)")
    public DeptmentVO getDepTreeTest(@ApiParam("父节点code") @RequestParam(value = "parentcode", required = false) String parentcode) {
        return flowUserInfoService.getDepTree(parentcode);
    }
//    public DeptmentVO getDepTreeTest() {
//        String deptmentStr = getMockData("department.txt");
//        if (StringUtils.isEmpty(deptmentStr))
//            return null;
//        ObjectMapper om = new ObjectMapper();
//        try {
//            DeptmentVO deptment = om.readValue(deptmentStr, DeptmentVO.class);
//            return deptment;
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    @GetMapping("/getRoleList/test")
    @ApiOperation("获取角色列表(测试)")
    public List<RoleVO> getRoleListTest(@ApiParam("角色名称") @RequestParam(value = "name", required = false) String name) {
        return flowUserInfoService.getRoleList(name);
    }
    /*public List<RoleVO> getRoleListTest() {
        String roleStr = getMockData("role.txt");
        if (StringUtils.isEmpty(roleStr))
            return null;
        ObjectMapper om = new ObjectMapper();
        try {
            List<RoleVO> roles = om.readValue(roleStr, new TypeReference<List<RoleVO>>() {
            });
            return roles;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }*/


    @GetMapping("/getFlowRuleList/test")
    @ApiOperation("获取流程规则(测试)")
    public List<RuleVO> getFlowRuleListTest() {
        return flowUserInfoService.getFlowRuleList();
    }
    /*public List<RuleVO> getFlowRuleListTest() {
        String ruleStr = getMockData("rule.txt");
        if (StringUtils.isEmpty(ruleStr))
            return null;
        ObjectMapper om = new ObjectMapper();
        try {
            List<RuleVO> rules = om.readValue(ruleStr, new TypeReference<List<RuleVO>>() {
            });
            return rules;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }*/

    @GetMapping("/getUserList/test")
    @ApiOperation("获取用户列表(测试)")
    public List<UserVO> getUserListTest(@ApiParam("防止参数不传") @RequestParam(value = "userpara", required = false) String userpara,
                                    @ApiParam("企业id") @RequestParam(value = "uuid", required = false) String uuid,
                                    @ApiParam("部门编码") @RequestParam(value = "depcode", required = false) String depcode,
                                    @ApiParam("区域编码") @RequestParam(value = "areacode", required = false) String areacode,
                                    @ApiParam("角色名称（逗号隔开）") @RequestParam(value = "roles", required = false) String roles,
                                    @ApiParam("角色是否是与的关系（1:是，0：否）") @RequestParam(value = "isand", required = false) String isand,
                                    @ApiParam("用户类型（1:政府,2:企业）") @RequestParam(value = "usertype", required = false) String usertype,
                                    @ApiParam("流程启动用户loginame") @RequestParam(value = "startuser", required = false) String startuser,
                                    @ApiParam("流程当前用户loginame") @RequestParam(value = "flowuser", required = false) String flowuser,
                                    @ApiParam("规则") @RequestParam(value = "rulecode", required = false) String rulecode) {
        return flowUserInfoService.getUserList(userpara, uuid, depcode, areacode, roles, isand, usertype, startuser, flowuser, rulecode);
    }
   /*public List<UserVO> getUserListTest() {
        String userStr = getMockData("user.txt");
        if (StringUtils.isEmpty(userStr))
            return null;
        ObjectMapper om = new ObjectMapper();
        try {
            List<UserVO> users = om.readValue(userStr, new TypeReference<List<UserVO>>() {
            });
            return users;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }*/


    private String getMockData(String path) {
        InputStream ins = this.getClass().getClassLoader().getResourceAsStream(path);
        if (ins == null)
            return null;
        try {
            BufferedReader tBufferedReader = new BufferedReader(new InputStreamReader(ins));
            StringBuilder tStringBuffer = new StringBuilder();
            String sTempOneLine;
            while ((sTempOneLine = tBufferedReader.readLine()) != null) {
                tStringBuffer.append(sTempOneLine);
            }
            return tStringBuffer.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
